package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Code;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分类分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Result page(Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        Page page1 = categoryMapper.selectPage(new Page<>(page, pageSize), wrapper);
        return new Result(Code.OK, page1,"{}");
    }

    /**
     * 添加分类
     */
    @Override
    public Result add(HttpServletRequest request, @RequestBody Category category) {
        // 2、判断类型（name）是否存在
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
             wrapper.eq("name", category.getName());
        Category one = categoryMapper.selectOne(wrapper);
        // 2.1设置条件
        // 2.2判断是否存在
        if (one == null){
              category.setCreateTime(System.currentTimeMillis());
            int num = categoryMapper.insert(category);
            if (num>0){
                return new Result(Code.OK, "添加成功", "{}");
            }
                return new Result(Code.ERR401, "添加失败请联系管理员", "{}");
        }
        return new Result(Code.ERR401, "'分类名'已存在", "{}");
        // 2.3存在 结束方法
        // 2.4不存在 则执行添加
        // 3、获取创建时间
        // 4、添加
    }

    /**
     * 删除分类
     */
    @Override
    public Result delect(String id){
        int i = categoryMapper.deleteById(id);
        if (i>0){
            return new Result(Code.OK, "删除成功", "{}");
        }
        return new Result(Code.ERR401, "删除失败请联系管理员", "{}");
    }


    /**
     * 编辑分类
     */
    @Override
    public Result put(Category category){
        int i = categoryMapper.updateById(category);
        if (i>0){
            return new Result(Code.OK, null, "修改分类信息成功", "{}");
        }
        return new Result(Code.ERR401,"修改失败","{}");
    }

    /**
     * 菜品下拉列表
     */
    @Override
    public Result list(Integer type){
        List<Category> list = categoryMapper.selectList(new QueryWrapper<Category>().eq("type", type));

        if(list.size() != 0){
            return new Result(Code.OK, null, list, "{}");
        }
        return new Result(Code.OK, "暂无数据", null,null);
    }


    /**
     * 移动端分类查询
     * @return
     */
    @Override
    public Result lists() {
        List<Category> list = categoryMapper.selectList(null);
        return new Result(Code.OK,list,"{}");
    }
}


